package com.atguigu.utils;

import com.alibaba.druid.pool.DruidPooledConnection;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.common.GmallConfig;
import org.apache.commons.lang3.StringUtils;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Set;

/**
 * @className: PhoenixUtil
 * @author: LinCong
 * @description:
 * @date: 2023/1/14 12:35
 * @version: 1.0
 */
public class PhoenixUtil {

    public static void upsertValues(DruidPooledConnection connection, String sinkTable, JSONObject data) throws SQLException {
//        1、拼接sql语句 upsert db.tn(id,name.sex) valus('1001','zhangsan','male')
        Set<String> columns = data.keySet();
        Collection<Object> values = data.values();
        String sql="upsert into "+ GmallConfig.HBASE_SCHEMA+"."+sinkTable+"("
                +StringUtils.join(columns,",")+")"
                +" values('"+StringUtils.join(values,"','")+"')";
        System.out.println("写入维度表sql语句："+sql);
//        2、预编译sql
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

//        3、执行
        preparedStatement.execute();
        connection.commit();
//        4、释放资源
        if (preparedStatement!=null){
            preparedStatement.close();
        }
    }
}
